/*
 * @# CacheAndDbMultiThreadCase.java 2011-11-7 下午07:15:33
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.experiments.redis;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.lang.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author alvin hwang
 */
public class CacheAndDbMultiThreadCase {

	private static Logger log = LoggerFactory.getLogger(CacheAndDbMultiThreadCase.class);
	
	class Task implements Runnable {
		private int loops;
		private boolean same;

		public Task(int loops, boolean same) {
			this.loops = loops;
			this.same = same;
		}

		@Override
		public void run() {
			CacheAndDbCase cacheAndDbCase = new CacheAndDbCase();
			cacheAndDbCase.run(loops, same);
		}
	}

	public void run(int threadCount, int requestCount, boolean same) {
		ExecutorService executorService = Executors.newFixedThreadPool(threadCount);

		StopWatch stopWatch = new StopWatch();
		stopWatch.start();

		for (int i = 0; i < threadCount; i++) {
			executorService.execute(new Task(requestCount, same));
		}

		executorService.shutdown();
		while (!executorService.isTerminated()) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		stopWatch.stop();
		log.debug("CacheAndDbMultiThreadCase 线程数: {} 遍历次数: {} 总运行时间为: " + stopWatch.toString(), threadCount,
				requestCount);
	}
	
	public static void main(String[] args) {
		CacheAndDbMultiThreadCase multiThreadCase = new CacheAndDbMultiThreadCase();
		multiThreadCase.run(100, 1000, false);
	}
}
